import { getDraftList, setContent, getContent, createColumn } from './service';
import { getCookie } from '@/utils/utils';
import { message } from 'antd';
import config from '@/../config';

export default {
  namespace: 'draftModel',
  state: {
    //可收藏多种网课列表
    data: {},
    isCollection: false, //当前bvid是否已收藏
    csrf: '', //登录token
    aid: '', //草稿的aid
  },
  reducers: {
    //统一保存数据用法
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
  effects: {
    //获取我的学习列表的专栏id
    *getId({ payload }, { call, put }) {
      let id = 0;
      const draftList = yield call(getDraftList, payload);
      const data = draftList;
      if (data.artlist) {
        const { drafts } = data.artlist || [];
        for (let i = 0; i < drafts.length; i++) {
          if (drafts[i].title === config.myOnlineClassName) {
            id = drafts[i].id;
          }
        }
      }
      //若没有这个专栏 创建
      if (id === 0) {
        const { csrf } = payload;
        const newId_promise = yield put({
          type: 'createColumn',
          payload: { csrf },
        });
        id = yield newId_promise;
      }
      return id;
    },
    //创建专栏为我的学习列表
    *createColumn({ payload }, { call }) {
      const column = yield call(createColumn, payload);
      const data = column;
      if (data.code === 0) {
        const { aid = '' } = data.data;
        return aid;
      } else {
        return 0;
      }
    },
    //获取网课收藏的数据
    *getCollectionList({ _ }, { call, put }) {
      const csrf = getCookie('bili_jct');
      const getId_promise = yield put({
        type: 'getId',
        payload: { csrf },
      });
      const aid = yield getId_promise; //aid
      const res = yield call(getContent, { aid });
      //将获取到的token和aid保存到内存
      yield put({
        type: 'save',
        payload: { csrf, aid },
      });
      if (res.data) {
        let { content = '' } = res.data;
        content = content
          .replace('<p>', '')
          .replace('</p>', '')
          .replace(/&#34;/g, '"');
        if (content === '') {
          console.log('没有网课数据，去收藏');
          return;
        }
        const data = JSON.parse(content);
        // console.log('数据', data);
        //将拿到的数据保存到内存
        yield put({
          type: 'save',
          payload: { data },
        });
        return;
      } else {
        console.log('读取网课失败，接口可能有问题');
        return;
      }
    },
    //将网课视频添加到学习列表中
    *add({ payload }, { call, put, select }) {
      const { dqVideo, type } = payload;
      const { data, csrf, aid } = yield select((state) => state.draftModel);
      const list = data[type];
      list.push(dqVideo);
      const res = yield call(setContent, { data, csrf, aid });
      if (res.code === 0) {
        message.success('收藏网课成功');
        yield put({
          type: 'save',
          payload: { isCollection: true },
        });
      }
      // console.log(data)
    },
    //判断当前bvid是否已收藏
    *isCollection({ payload }, { call, put, select }) {
      const { bvid, p } = payload;
      const { data, csrf, aid } = yield select((state) => state.draftModel);
      let isCollection = false; //是否已收藏
      let _filterData = [];
      for (let key in data) {
        const filterData = data[key].filter((d) => d.bvid === bvid); //筛选出是否存在了bvid
        if (filterData.length > 0) {
          isCollection = true;
          _filterData = filterData;
          break;
        }
      }
      // console.log('bvid是否已收藏', isCollection);
      if (isCollection) {
        //bvid已收藏
        if (_filterData[0].p != p) {
          // console.log('切换了分p，更新该网课记录');
          _filterData[0].p = p;
          const res = yield call(setContent, { data, csrf, aid });
          if (res.code === 0) {
            console.log('更新了网课播放记录');
          }
        }
      }
      yield put({
        type: 'save',
        payload: { isCollection },
      });
    },
  },
};
